<?php

namespace App\Admin\Controllers;

use App\Models\ProCategory;
use App\Models\ProProducts;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ProProductsController extends Controller
{
    use ModelForm;

    const header = '内容管理';
    const description = '';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(self::header);
            $content->description(self::description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(self::header);
            $content->description(self::description);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(self::header);
            $content->description(self::description);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ProProducts::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->column('topic', '标题');
            $grid->column('category.topic', '分类');
            $grid->column('createtime', '发布时间');
//            $grid->column('paixu', '排序')->sortable();

            $grid->disableExport();
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->like('title', '标题');

                $filter->is('categoryid', '分类')->select(ProCategory::all()->pluck('topic', 'id'));

            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ProProducts::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select('categoryid', '分类')->options(ProCategory::all()->pluck('topic', 'id'));

            $form->text('topic', '标题');
//            $form->text('video', '视频地址')->help('如需修改请贴入新的视频分享');
//            $form->text('specialty', '资料下载地址')->help('资料下载选填');
//            $form->number('paixu', '排序');
//            $form->image('bigpic', '图片');

            $form->datetime('createtime', '发布时间');
            $form->editor('content', '内容')->attribute(['style' => 'height:430px;',])->help('描述任务完成情况，现场图文等等');
        });
    }
}
